import httpInstance from "@/utils/http";

// 获取分类
export async function getCategories() {
  try {
    const res = await httpInstance.get('/api/category');
    return res.data;
  } catch (error) {
    console.error('Error fetching categories:', error);
    throw error;
  }
}

// 根据父类分类id 获取子分类
export async function getSubCategories(parentId) {
  try {
    const res = await httpInstance.post(`/api/subcategories/${parentId}`);
    return res.data;
  } catch (error) {
    console.error('Error fetching subcategories:', error);
    throw error;
  }
}

// 获取轮播图
export async function getCarousel() {
  try {
    const res = await httpInstance.get('/api/banner');
    return res.data;
  } catch (error) {
    console.error('Error fetching carousel:', error);
  }
}

// 获取热销产品
export async function getHotProducts(size, current) {
  try {
    const body = {
      size,
      current
    };
    const res = await httpInstance.post('/api/product/getHot', body);
    return res.data;
  } catch (error) {
    console.error('Error fetching hot products:', error);
  }
}

// 获取商品列表
export async function getproductsList(current, size) {
  try {
    const res = await httpInstance.post('/api/product', {
      size,
      current
    });
    return res.data;
  } catch (error) {
    console.error('Error fetching products list:', error);
    throw error;
  }
}

// 根据分类id获取商品列表
export async function getProductsByCategoryId(categoryId, size = 8, current = 1) {
  try {
    const body = {
      size,
      current
    };
    const res = await httpInstance.post(`/api/product/category/parent/${categoryId}`, body);
    return res.data;
  } catch (error) {
    console.error('Error fetching products by category id:', error);
  }
}

// 根据子分类id获取商品列表
export async function getProductsBySubCategoryId(subCategoryId, size = 8, current = 1) {
  try {
    const body = {
      size,
      current
    };
    const res = await httpInstance.post(`/api/product/category/${subCategoryId}`, body);
    return res.data;
  } catch (error) {
    console.error('Error fetching products by category id:', error);
  }
}

// 搜索商品
export async function searchProducts(keyword, size = 8, current = 1) {
  try {
    const body = {
      size,
      current
    };
    const url = `/api/product/search?keyword=${keyword}`;
    const res = await httpInstance.post(url, body);
    return res.data;
  } catch (error) {
    console.error('Error searching products:', error);
  }
}

// 获取商品详情
export async function getproduct(productId) {
  try {
    const res = await httpInstance.get(`/api/product/${productId}`);
    return res.data;
  } catch (error) {
    console.error('Error fetching product:', error);
    throw error;
  }
}

// 新增商品
export async function addProduct(product) {
  try {
    const res = await httpInstance.post('/api/product/add', product);
    return res.data;
  } catch (error) {
    console.error('Error adding product:', error);
    throw error;
  }
}

// 删除商品
export const deleteProduct = (id) => {
  return httpInstance({
    url: `/api/product/${id}`,
    method: 'delete'
  });
};

// 更新商品
export async function updateProduct(product) {
  try {
    const res = await httpInstance.put('/api/product/update', product);
    return res.data;
  } catch (error) {
    console.error('Error updating product:', error);
    throw error;
  }
}


